$(function () {
  let layer = layui.layer;

  initZan();
  function initZan() {
    axios({
      method: 'get',
      url: 'http://124.223.14.236:8060/home/user/userDataList',
      headers: {
        token: localStorage.getItem('token'),
      },
      params: {
        type: 2,
        page: 1,
        limit: 10,
      },
    }).then(function (res) {
      console.log(res);
      if (res.status !== 200) {
        return layer.msg('初始化我的点赞失败！');
      }
      let Zan_html;
      if (res.data.data.list.data.length <= 0) {
        Zan_html = `<p style="font-size='20px'">您还未点赞</p>`;
        $('.items-01').html(Zan_html);
      } else {
        Zan_html = res.data.data.list.data.map(item => {
          return `
          <li>
            <a /*target="_blank"*/ href="javascript:;">
              <span class="thumbnail">
                <img class="thumb" src="http://124.223.14.236:8060/${item.pic}" />
              </span>
              <span class="text">${item.title}</span>
              <span class="text-muted post-views">已点赞(${item.click})</span>
              <button class="btn btn-xs btn-danger" data-id="${item.id}"><i class="fa fa-trash"></i>&nbsp;取消点赞</button>
            </a>
          </li>
        `;
        });
        $('.items-01').html(Zan_html.join(''));
      }
    });
  }

  /* // 给取消点赞绑定委托代理点击事件
  $('.items-01').on('click', '.btn-xs', function () {
    let index = $(this).attr('data-id');
    axios({
      method: 'get',
      url: 'http://124.223.14.236:8060/home/user/userDataHandle',
      headers: {
        token:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6MTI2LCJ1c2VybmFtZSI6IuS4gOWJquaihSIsIm1vYmlsZSI6IjE4MjY3NzIzNDY1Iiwic2V4IjowLCJwYXNzd29yZCI6IiIsImljb24iOiIiLCJub3RlcyI6IuinguaNruWHuua7oeinguS4g-WIsOS4juWTjeS4pOi-vuWHhuavj-WImemimOW8gOmDqOacuuacr-eOsOWbvemhu-S4lOa7oeiusOOAgiIsImJpcnRoZGF5IjoiMjAxMS0wOC0wMyIsImFkZHJlc3MiOiLmtZnmsZ_nnIEg5rip5bee5biCIOW5s-mYs-WOvyJ9LCJpYXQiOjE2MzU4NjY1MDZ9.G7k3SuNcfFEZEjQgZB33kKhgJiXCwcOy7ZoD1QJJXoo',
      },
      params: {
        type: 2,
        article_id: index,
        action: 'del',
      },
    }).then(function (res) {
      console.log(res);
      if (res.status !== 200) {
        return layer.msg('初始化我的点赞失败！');
      }
      initZan();
    });
  }); */

  // 给取消点赞绑定委托代理点击事件
  $('.items-01').on('click', '.btn-xs', function () {
    let id = $(this).attr('data-id');
    // 提示用户是否要删除
    layer.confirm('确认取消?', { icon: 3, title: '提示' }, function (index) {
      axios({
        method: 'get',
        url: 'http://124.223.14.236:8060/home/user/userDataHandle',
        headers: {
          token: localStorage.getItem('token'),
        },
        params: {
          type: 2,
          article_id: id,
          action: 'del',
        },
      }).then(function (res) {
        console.log(res);
        if (res.status !== 200) {
          return layer.msg('取消失败！');
        }
        layer.msg('取消成功！');
        layer.close(index);
        initZan();
      });
    });
  });
  homePage();
  // 首页和全局数据
  function homePage() {
    axios({
      method: 'GET',
      url: 'http://124.223.14.236:8060/home/index/index',
    }).then(function (res) {
      // console.log(res);
      // 导航栏
      let strNav = res.data.data.allCate
        .map(item => {
          return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
        })
        .join('');
      $('#nav').append(strNav);
    });
  }
  userCenter();
  // 用户中心和退出
  function userCenter() {
    // 没有token的时候隐藏用户中心和退出
    if (localStorage.getItem('token')) {
      let str = `
         <a href="user.html" id="user"><i class="fa fa-user"></i>&nbsp;用户中心</a>
         <a href="javascript:;" class="quit"><i></i>&nbsp;退出</a>`;
      $('.slinks').html(str);
    } else {
      let str = `
          <a href="login.html"><i class="fa fa-hand-o-right"></i>&nbsp;登录</a> |
          <a href="reg.html"><i class="fa fa-registered"></i>&nbsp;注册</a>`;
      $('.slinks').html(str);
    }
    $('#user').on('click', function (e) {
      e.preventDefault();
      if (localStorage.getItem('token')) {
        location.href = 'user.html';
      } else {
        location.href = 'login.html';
      }
    });
    $('.quit').on('click', function (e) {
      layer.confirm('你确定要退出吗?', { icon: 3, title: '退出提示' }, function (index) {
        localStorage.removeItem('token');
        location.reload();

        layer.close(index);
      });
    });
  }
  // 导航栏跳转
  function getNavSkip() {
    $('#nav').on('click', 'a', function (e) {
      e.preventDefault();
      window.open('../list.html?id=' + $(this).attr('id'));
    });
  }
  getNavSkip();
});
